//
//  ZGLExtractCashController.m
//  ZeegingLottery
//
//  Created by 蒂埃里 on 2018/8/14.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLExtractCashController.h"
#import "WLDecimalKeyboard.h"

@interface ZGLExtractCashController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *bankCardBgView;
@property (nonatomic, strong) UIView *phoneNumBgView;
@property (nonatomic, strong) UILabel *bankCardNumLabel;
@property (nonatomic, strong) UILabel *bankCard;
@property (nonatomic, strong) UILabel *cashLabel;
@property (nonatomic, strong) UITextField *cashTF;
@property (nonatomic, strong) UIButton *takeAllMoneyBtn;
@property (nonatomic, strong) UILabel *phoneNumLable;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UIButton *VerifiCodeBtn;
@property (nonatomic, strong) UILabel *VerifiCodeLabel;
@property (nonatomic, strong) UITextField *VerifiCodeTF;
@property (nonatomic, strong) UIButton *doneBtn;
@property (nonatomic, strong) WLDecimalKeyboard *extractCashKeyboard;

@property (nonatomic, copy) NSString *phoneNumString;
@property (nonatomic, copy) NSString *balanceString; //可提现金额

@end

@implementation ZGLExtractCashController

- (WLDecimalKeyboard *)extractCashKeyboard
{
    if (!_extractCashKeyboard) {
        _extractCashKeyboard = [[WLDecimalKeyboard alloc]initWithTintColor:globalRed];
    }
    return _extractCashKeyboard;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    self.view.backgroundColor = ViewBackgroundColor;
    
    [self setupUI];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self getExtractCashInfo];
    });
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer
                                      *)gestureRecognizer{
    if (self.isSlide) {
        return YES;
    }else{
        return NO;
    }
    
//    return NO; //YES：允许右滑返回  NO：禁止右滑返回
}

#pragma mark - method

- (void)takeAllMoneyBtnClick:(UIButton *)sender
{
    DLog(@"全部提现");
    [TalkingData trackEvent:ZGLExtractingBtnEventID label:@"takeAll"];

    self.cashTF.text = self.balanceString;
    
}

- (void)getVerifiCode:(UIButton *)sender
{
    DLog(@"发送短信验证请求");
    [TalkingData trackEvent:ZGLExtractingBtnEventID label:@"getVerifiCode"];
    
    BOOL isValidateMobile = false;
    NSString *phoneNum = sZGLUserInfoModel.mobile;
    isValidateMobile = [PublicMethod isValidateMobile:phoneNum];
    if (isValidateMobile) {
        
        //发送请求
        [sZGLHTTPRequestManager getCertificationVerticodeWithSMSType:@"3" Success:^{
            
            [EasyTextView showSuccessText:@"短信以发送，请查收"];
            [sender startCountdownTime:countDownTime withCountDownBlock:nil];//倒计时
            
        } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {
            
            [EasyTextView showErrorText:@"短信发送失败，请检查网络"];
        }];
        
    }else{
        
        [EasyTextView showInfoText:@"请输入正确的手机号"];
    }
}

- (void)doneBtnClick:(UIButton *)sender
{
    DLog(@"完成，提交请求");
    NSString *cash = _cashTF.text;
    
    NSString *chargeLabel = sZGLUserInfoModel.mobile;
    NSDictionary *parame = @{
                             @"extractingMoney":cash
                             };
    [TalkingData trackEvent:ZGLExtractingBtnEventID label:chargeLabel parameters:parame];
    
    
    [cash integerValue];
    NSString *code = _VerifiCodeTF.text;
    
    if (cash.length>0 && [cash integerValue] >= 100) {
        
        if (code.length >0) {
            //菊花
            [EasyLoadingView showLoadingText:@"" config:^EasyLoadingConfig *{
                
                return [EasyLoadingConfig shared].setLoadingType(LoadingShowTypeIndicator);
            }];
            WS(weakself)
            [sZGLHTTPRequestManager extractCashComfirmWithCash:cash VertiCode:code Success:^(id responsedata) {
                
                NSString *code = responsedata[@"code"];
                DLog(@"提现成功");
                if ([code integerValue]==1) {
                    //                    [EasyTextView showSuccessText:@"提现成功"];
                    [EasyLoadingView hidenLoading];
                    NSDictionary *data = responsedata[@"data"];
                    NSString *time = data[@"time"];
                    
                    ZGLSuccessCallBackVC *callBackVC = [[ZGLSuccessCallBackVC alloc]init];
                    callBackVC.statusMessageLabel.text = @"提现申请已提交";
                    callBackVC.subMessageLabel.text = [NSString stringWithFormat:@"预计到账时间：%@",time];
                    callBackVC.businessModeLabel.text = @"银行卡";
                    callBackVC.AmountLabel.text = @"金额";
                    callBackVC.businessDetailLablel.text = self.bankCardNumString;
                    callBackVC.Amount.text = [NSString stringWithFormat:@"￥%@元",cash];
                    [callBackVC initWithCallBackStatus:ZGLBusinessCallBackExtractCash Status:ZGLStatusSuccess];
                    [weakSelf cw_pushViewController:callBackVC drewerHiddenDuration:0.01];
                    
                    
                }else if([code integerValue]==0){
                    [EasyLoadingView hidenLoading];
                    [EasyTextView showInfoText:@"输入金额大于可提现金额"];
                }
                
            } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {
                [EasyLoadingView hidenLoading];
                [EasyTextView showInfoText:@"提现失败，请重试"];
            }];
            
        }else{
            DLog(@"请获取验证并输入")
            [EasyTextView showInfoText:@"请获取验证并输入"];
        }
    }else{
        DLog(@"请输入提现金额");
        [EasyTextView showInfoText:@"请输入提现金额"];
    }
    
}

//获取提现信息
- (void)getExtractCashInfo
{
    WS(weakself)
    [sZGLHTTPRequestManager getBankCardInfoWithSuccess:^(id responsedata) {

//        weakSelf.balance = responsedata[@"balance"];
//        weakSelf.realName = responsedata[@"real_name"];
//        weakSelf.phoneNum = responsedata[@"mobile"];
//        weakSelf.bankCardNum = responsedata[@"num"];
//        DLog(@"realName-%@,phoneNum-%@,bankCardNum-%@",_realName,_phoneNum,_bankCardNum);
//        NSString *idCardNumString;
//        NSString *phoneNumString;
//        NSString *balanceString; //可提现金额

        weakSelf.bankCardNumString = responsedata[@"num"];
        weakSelf.balanceString = responsedata[@"balance"];
        weakSelf.phoneNumString = responsedata[@"mobile"];
        _bankCard.text = _bankCardNumString;
        NSString *balanceStr = [PublicMethod DigitalNumberWithString:_balanceString];
        _cashTF.placeholder = [NSString stringWithFormat:@"可提金额 ￥%@",balanceStr];
        _phoneLabel.text = _phoneNumString;

    } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {

        DLog(@"获取提现信息失败");
    }];
}

#pragma mark - setupUI

- (void)goBack
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)setupUI
{
    UIView *bankCardBgView = [[UIView alloc]init];
    bankCardBgView.backgroundColor = [UIColor whiteColor];
    self.bankCardBgView = bankCardBgView;
    [self.view addSubview:_bankCardBgView];
    [_bankCardBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(30);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        
//        make.height.equalTo(@(100));
        
    }];
    
    [HKUIUtils addLineInView:_bankCardBgView top:YES leftMargin:0 rightMargin:0];
    
    UILabel *bankCardNumLabel = [[UILabel alloc]init];
    bankCardNumLabel.text = @"银行卡";
    bankCardNumLabel.font = [UIFont systemFontOfSize:14];
    bankCardNumLabel.textColor = mainTextColor;
    bankCardNumLabel.textAlignment = 0;
    self.bankCardNumLabel = bankCardNumLabel;
    [_bankCardBgView addSubview:_bankCardNumLabel];
    [_bankCardNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_bankCardBgView).offset(15);
        make.left.equalTo(_bankCardBgView).offset(20);
        make.width.equalTo(@(60));
        
    }];
    
    UILabel *bankCard = [[UILabel alloc]init];
//    bankCard.text = @"123456789012345678";
    bankCard.text = self.bankCardNumString;
    bankCard.font = [UIFont systemFontOfSize:14];
    bankCard.textColor = mainTextColor;
    bankCard.textAlignment = 0;
    self.bankCard = bankCard;
    [_bankCardBgView addSubview:_bankCard];
    [_bankCard mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_bankCardNumLabel.mas_right).offset(10);
        make.centerY.equalTo(_bankCardNumLabel);
        make.right.equalTo(_bankCardBgView).offset(-20);
        
    }];
    
    UIView *line1 = [[UIView alloc]init];
    line1.backgroundColor = BorderColor;
    [_bankCardBgView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_bankCardNumLabel.mas_bottom).offset(15);
        make.left.equalTo(_bankCardNumLabel);
        make.right.equalTo(_bankCardBgView);
        make.height.equalTo(@(kBorderWidth));
    }];
    
    
    UILabel *cashLabel = [[UILabel alloc]init];;
    cashLabel.text = @"提现金额";
    cashLabel.font = [UIFont systemFontOfSize:14];
    cashLabel.textColor = mainTextColor;
    cashLabel.textAlignment = 0;
    self.cashLabel = cashLabel;
    [_bankCardBgView addSubview:_cashLabel];
    [_cashLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(line1.mas_bottom).offset(15);
        make.left.equalTo(_bankCardBgView).offset(20);
        make.width.equalTo(_bankCardNumLabel);
        
    }];
    
    UIButton *takeAllMoneyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [takeAllMoneyBtn setTitle:@"全部提现" forState:UIControlStateNormal];
    [takeAllMoneyBtn setTitleColor:globalRed forState:UIControlStateNormal];
    takeAllMoneyBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    takeAllMoneyBtn.titleLabel.textAlignment = 2;
    [takeAllMoneyBtn addTarget:self action:@selector(takeAllMoneyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.takeAllMoneyBtn = takeAllMoneyBtn;
    [_bankCardBgView addSubview:_takeAllMoneyBtn];
    [_takeAllMoneyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(_cashLabel);
        make.right.equalTo(_bankCard);
//        make.left.equalTo(_cashTF.mas_right);
        make.width.equalTo(@(55));
        
    }];
    
    UITextField *cashTF = [[UITextField alloc]init];
    cashTF.delegate = self;
    cashTF.inputView = self.extractCashKeyboard;
    [cashTF reloadInputViews];
    
    NSString *balanceStr = [PublicMethod DigitalNumberWithString:self.balanceString];
    cashTF.placeholder = [NSString stringWithFormat:@"可提金额 ￥%@",balanceStr];
    cashTF.textAlignment = 0;
    [cashTF placeholderRectForBounds:CGRectMake(0, 0, 20, 200)];
    cashTF.textColor = mainTextColor;
    cashTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    cashTF.font = [UIFont systemFontOfSize:btnFontSize];
    self.cashTF = cashTF;
    [_bankCardBgView addSubview:_cashTF];
    [_cashTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_cashLabel.mas_right).offset(10);
        make.centerY.equalTo(_cashLabel);
        make.right.equalTo(_takeAllMoneyBtn.mas_left).offset(-5);
//        make.right.equalTo(_takeAllMoneyBtn.mas_left);
//        make.width.equalTo(@(145));
    }];
    
    
    [HKUIUtils addLineInView:_bankCardBgView top:NO leftMargin:0 rightMargin:0];
    
    [_bankCardBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(_cashLabel.mas_bottom).offset(15);
    }];
    
    
    UIView *phoneNumBgView = [[UIView alloc]init];
    phoneNumBgView.backgroundColor = [UIColor whiteColor];
    self.phoneNumBgView = phoneNumBgView;
    [self.view addSubview:_phoneNumBgView];
    [_phoneNumBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_bankCardBgView.mas_bottom).offset(30);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        
//                make.height.equalTo(@(100));
        
    }];
    
    [HKUIUtils addLineInView:_phoneNumBgView top:YES leftMargin:0 rightMargin:0];
    
    UILabel *phoneNumLable = [[UILabel alloc]init];
    phoneNumLable.text = @"手机号";
    phoneNumLable.font = [UIFont systemFontOfSize:14];
    phoneNumLable.textColor = mainTextColor;
    phoneNumLable.textAlignment = 0;
    self.phoneNumLable = phoneNumLable;
    [_phoneNumBgView addSubview:_phoneNumLable];
    [_phoneNumLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_phoneNumBgView).offset(15);
        make.left.equalTo(_phoneNumBgView).offset(20);
        make.width.equalTo(_bankCardNumLabel);
        
    }];
    
    UILabel *phoneLabel = [[UILabel alloc]init];
//    NSString *phoneNum = [PublicMethod phoneNumToAsterisk:sZGLUserInfoModel.mobile];
    NSString *phoneNum = self.phoneNumString;
    phoneLabel.text = phoneNum;
    phoneLabel.font = [UIFont systemFontOfSize:14];
    phoneLabel.textColor = mainTextColor;
    phoneLabel.textAlignment = 0;
    self.phoneLabel = phoneLabel;
    [_phoneNumBgView addSubview:_phoneLabel];
    [_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_phoneNumLable.mas_right).offset(10);
        make.centerY.equalTo(_phoneNumLable);
//        make.right.equalTo(_phoneNumBgView).offset(-20);
        
    }];
    
    UIButton *VerifiCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [VerifiCodeBtn setTitle:@"获取短信验证码" forState:UIControlStateNormal];
    [VerifiCodeBtn setTitleColor:globalRed forState:UIControlStateNormal];
    VerifiCodeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    VerifiCodeBtn.titleLabel.textAlignment = 2;
    [VerifiCodeBtn addTarget:self action:@selector(getVerifiCode:) forControlEvents:UIControlEventTouchUpInside];
    self.VerifiCodeBtn = VerifiCodeBtn;
    [_phoneNumBgView addSubview:_VerifiCodeBtn];
    [_VerifiCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(_phoneLabel);
        make.right.equalTo(_takeAllMoneyBtn);
        make.left.equalTo(_phoneLabel.mas_right).offset(10);
        
    }];
    
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = BorderColor;
    [_phoneNumBgView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_phoneNumLable.mas_bottom).offset(15);
        make.left.equalTo(_phoneNumLable);
        make.right.equalTo(_phoneNumBgView);
        make.height.equalTo(@(kBorderWidth));
    }];
    
    
    UILabel *VerifiCodeLabel = [[UILabel alloc]init];
    VerifiCodeLabel.text = @"验证码";
    VerifiCodeLabel.font = [UIFont systemFontOfSize:14];
    VerifiCodeLabel.textColor = mainTextColor;
    VerifiCodeLabel.textAlignment = 0;
    self.VerifiCodeLabel = VerifiCodeLabel;
    [_phoneNumBgView addSubview:_VerifiCodeLabel];
    [_VerifiCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(line2.mas_bottom).offset(15);
        make.left.equalTo(_phoneNumLable);
        make.width.equalTo(_phoneNumLable);
        
    }];
    
    UITextField *VerifiCodeTF = [[UITextField alloc]init];
    VerifiCodeTF.placeholder = @"请输入短信验证码";
    VerifiCodeTF.textAlignment = 0;
    VerifiCodeTF.textColor = mainTextColor;
    VerifiCodeTF.font = [UIFont systemFontOfSize:13];
    VerifiCodeTF.keyboardType = UIKeyboardTypeNumberPad;
    VerifiCodeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.VerifiCodeTF = VerifiCodeTF;
    [_phoneNumBgView addSubview:_VerifiCodeTF];
    [_VerifiCodeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_VerifiCodeLabel.mas_right).offset(10);
        make.centerY.equalTo(_VerifiCodeLabel);
        make.right.equalTo(_phoneNumBgView).offset(-20);
    }];
    
    [HKUIUtils addLineInView:_phoneNumBgView top:NO leftMargin:0 rightMargin:0];
    
    [_phoneNumBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(_VerifiCodeLabel.mas_bottom).offset(15);
    }];
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [doneBtn setBackgroundColor:globalRed];
    doneBtn.layer.cornerRadius = 3;
    doneBtn.layer.masksToBounds = YES;
    [doneBtn addTarget:self action:@selector(doneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.doneBtn = doneBtn;
    [self.view addSubview:_doneBtn];
    [_doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_phoneNumBgView.mas_bottom).offset(30);
        make.centerX.equalTo(self.view);
        make.width.equalTo(@(UI_SCREEN_WIDTH-40));
        make.height.equalTo(@(40));
    }];
    
}


#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
